{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "只能输入yes或no\n",
      "56.0\n",
      "不是合法成绩\n",
      "56.0\n",
      "不是合法成绩\n"
     ]
    }
   ],
   "source": [
    "\n",
    "numbers = []\n",
    "while True:\n",
    "    x = input('请输入一个成绩：')\n",
    "    #异常处理结构，用来保证用户只能输入实数，可自行增加代码限制实数范围取值\n",
    "    try:     \n",
    "        #先把x转化成实数，然后追加到列表numbers尾部\n",
    "        numbers.append(float(x))\n",
    "    except:\n",
    "        print('不是合法成绩')\n",
    "\n",
    "        #下面的循环用来限制用户只能输入任意大小写的“yes”或者“no”\n",
    "    while True:\n",
    "        flag=input('是否继续输入成绩？(yes/no)：').lower()\n",
    "        if flag not in ('yes', 'no'):\n",
    "            print('只能输入yes或no')\n",
    "        else:\n",
    "            break\n",
    "    if flag=='no':\n",
    "        break\n",
    "\n",
    "    #计算平均分\n",
    "    print(sum(numbers)/len(numbers))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                  \n",
      "        *         \n",
      "        **        \n",
      "       ***        \n",
      "       ****       \n",
      "      *****       \n",
      "      ******      \n",
      "      *****       \n",
      "       ****       \n",
      "       ***        \n",
      "        **        \n",
      "        *         \n"
     ]
    }
   ],
   "source": [
    "n = int(input('输入一个整数：'))\n",
    "for i in range(n):\n",
    " print((('*' * i).center(n * 3))) #center()是字符串排版方法，居中对齐\n",
    "                                      #其中的参数n*3表示排版后字符串长度\n",
    "for i in range(n, 0, -1):\n",
    " print((('*' * i).center(n * 3)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, World!\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "def Cni(n, i): \n",
    "   if not (isinstance(n,int) and isinstance(i,int) and n>=i):\n",
    "      print('n and i must be integers and n must be >=i.')\n",
    "      return\n",
    "result = 1\n",
    "\n",
    "    # 使用i和n-i把1到n之间的自然数分成3个区间\n",
    "    # 使用Min表示i和n-i中较小的数，Max表示其中较大的数\n",
    "Min, Max = sorted((i, n-i))\n",
    "\n",
    "for i in range(n, 0, -1):\n",
    "     if i > Max:\n",
    "         result *= i\n",
    "     elif i <= Min:\n",
    "       result //= i\n",
    "def my_function():\n",
    "    result = \"Hello, World!\"\n",
    "    return result\n",
    "\n",
    "print(my_function())\n",
    "\n",
    "\n",
    "print(Cni(6,2))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1*1=1\t\n",
      "1*2=2\t2*2=4\t\n",
      "1*3=3\t2*3=6\t3*3=9\t\n",
      "1*4=4\t2*4=8\t3*4=12\t4*4=16\t\n",
      "1*5=5\t2*5=10\t3*5=15\t4*5=20\t5*5=25\t\n",
      "1*6=6\t2*6=12\t3*6=18\t4*6=24\t5*6=30\t6*6=36\t\n",
      "1*7=7\t2*7=14\t3*7=21\t4*7=28\t5*7=35\t6*7=42\t7*7=49\t\n",
      "1*8=8\t2*8=16\t3*8=24\t4*8=32\t5*8=40\t6*8=48\t7*8=56\t8*8=64\t\n",
      "1*9=9\t2*9=18\t3*9=27\t4*9=36\t5*9=45\t6*9=54\t7*9=63\t8*9=72\t9*9=81\t\n"
     ]
    }
   ],
   "source": [
    "for i in range(1, 10):\n",
    "    for j in range(1, i+1):\n",
    "        print('{}*{}={}\\t'.format(j, i, i*j), end='')\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n",
      "14\n",
      "21\n",
      "28\n",
      "42\n",
      "49\n",
      "56\n",
      "63\n",
      "77\n",
      "84\n",
      "91\n",
      "98\n"
     ]
    }
   ],
   "source": [
    "for i in range(1, 101):\n",
    "    if i % 7 == 0 and i % 5 != 0:\n",
    "        print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def Cni(n, i):\n",
    "    if not (isinstance(n,int) and isinstance(i,int) and n>=i):\n",
    "        print('n and i must be integers and n must be >=i.')\n",
    "        return\n",
    "    result = 1\n",
    "    # 使用i和n-i把1到n之间的自然数分成3个区间\n",
    "    # 使用Min表示i和n-i中较小的数，Max表示其中较大的数\n",
    "    Min,Max=sorted((i,n-i))\n",
    "    for i in range(n,0,-1):\n",
    "        if i >Max:\n",
    "            result*=i\n",
    "        elif i <=Min:\n",
    "            result//=i\n",
    "            return result\n",
    "        \n",
    "        print(Cni(6,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "873\n"
     ]
    }
   ],
   "source": [
    "n=int(input('输入一个自然数：'))\n",
    "#使用result记录阶乘结果，t表示每一项\n",
    "result,t=1,1\n",
    "for i in range(2,n+1):\n",
    "    #在前一项的基础上得到当前项\n",
    "    t*=i\n",
    "    #将当前项累加到阶乘结果中\n",
    "    result+=t\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "TclError",
     "evalue": "unknown option \"-iustify\"",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTclError\u001b[0m                                  Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[2], line 16\u001b[0m\n\u001b[0;32m     13\u001b[0m root[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mwidth\u001b[39m\u001b[38;5;124m'\u001b[39m]\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m200\u001b[39m\n\u001b[0;32m     15\u001b[0m \u001b[38;5;66;03m#在窗口上创建标签组件\u001b[39;00m\n\u001b[1;32m---> 16\u001b[0m labelName\u001b[38;5;241m=\u001b[39m\u001b[43mtkinter\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mLabel\u001b[49m\u001b[43m(\u001b[49m\u001b[43mroot\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m     17\u001b[0m \u001b[43m                        \u001b[49m\u001b[43mtext\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mUser Name:\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[0;32m     18\u001b[0m \u001b[43m                        \u001b[49m\u001b[43miustify\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtkinter\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mRIGHT\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m     19\u001b[0m \u001b[43m                        \u001b[49m\u001b[43manchor\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43me\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[0;32m     20\u001b[0m \u001b[43m                        \u001b[49m\u001b[43mwidth\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m80\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m     21\u001b[0m \u001b[38;5;66;03m#把组件放置到窗口上指定区域\u001b[39;00m\n\u001b[0;32m     22\u001b[0m labelName\u001b[38;5;241m.\u001b[39mplace(x\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m10\u001b[39m,y\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m5\u001b[39m,width\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m80\u001b[39m,height\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m20\u001b[39m)\n",
      "File \u001b[1;32mc:\\Users\\冯文博严\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\tkinter\\__init__.py:3236\u001b[0m, in \u001b[0;36mLabel.__init__\u001b[1;34m(self, master, cnf, **kw)\u001b[0m\n\u001b[0;32m   3218\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\u001b[38;5;28mself\u001b[39m, master\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, cnf\u001b[38;5;241m=\u001b[39m{}, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkw):\n\u001b[0;32m   3219\u001b[0m \u001b[38;5;250m    \u001b[39m\u001b[38;5;124;03m\"\"\"Construct a label widget with the parent MASTER.\u001b[39;00m\n\u001b[0;32m   3220\u001b[0m \n\u001b[0;32m   3221\u001b[0m \u001b[38;5;124;03m    STANDARD OPTIONS\u001b[39;00m\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m   3234\u001b[0m \n\u001b[0;32m   3235\u001b[0m \u001b[38;5;124;03m    \"\"\"\u001b[39;00m\n\u001b[1;32m-> 3236\u001b[0m     \u001b[43mWidget\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__init__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmaster\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mlabel\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcnf\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkw\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32mc:\\Users\\冯文博严\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\tkinter\\__init__.py:2647\u001b[0m, in \u001b[0;36mBaseWidget.__init__\u001b[1;34m(self, master, widgetName, cnf, kw, extra)\u001b[0m\n\u001b[0;32m   2645\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m k, v \u001b[38;5;129;01min\u001b[39;00m classes:\n\u001b[0;32m   2646\u001b[0m     \u001b[38;5;28;01mdel\u001b[39;00m cnf[k]\n\u001b[1;32m-> 2647\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtk\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcall\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m   2648\u001b[0m \u001b[43m    \u001b[49m\u001b[43m(\u001b[49m\u001b[43mwidgetName\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_w\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mextra\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_options\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcnf\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   2649\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m k, v \u001b[38;5;129;01min\u001b[39;00m classes:\n\u001b[0;32m   2650\u001b[0m     k\u001b[38;5;241m.\u001b[39mconfigure(\u001b[38;5;28mself\u001b[39m, v)\n",
      "\u001b[1;31mTclError\u001b[0m: unknown option \"-iustify\""
     ]
    }
   ],
   "source": [
    "import tkinter\n",
    "import tkinter.messagebox\n",
    "import os\n",
    "import os.path\n",
    "\n",
    "path=os.getenv('temp')\n",
    "filename=os.path.join(path,'tempfile.txt')\n",
    "\n",
    "#创建应用程序窗口\n",
    "root=tkinter.Tk()\n",
    "#定义窗口大小\n",
    "root['height']=140\n",
    "root['width']=200\n",
    "\n",
    "#在窗口上创建标签组件\n",
    "labelName=tkinter.Label(root,\n",
    "                        text='User Name:',\n",
    "                        iustify=tkinter.RIGHT,\n",
    "                        anchor='e',\n",
    "                        width=80)\n",
    "#把组件放置到窗口上指定区域\n",
    "labelName.place(x=10,y=5,width=80,height=20)\n",
    "\n",
    "#创建字符串变量和文本框组件，同时设置关联的变量\n",
    "#可以通过关联变量来读取或修改文本框内的文本\n",
    "varName=tkinter.StringVar(root,value='')\n",
    "entryName=tkinter.Entry(root,\n",
    "                        width=80,\n",
    "                        textvariable=varName)\n",
    "entryName.place(x=100,y=5,width=80,height=20)\n",
    "\n",
    "labeIPwd= tkinter.Label(root,\n",
    "                        text='User Pwd:',\n",
    "                        justify=tkinter.RIGHT,\n",
    "                        anchor='e',\n",
    "                        width=80)\n",
    "labeIPwd.place(x=10,y=30,width=80,height=20)\n",
    "\n",
    "#创建密码文本框\n",
    "varPwd=tkinter.StringVar(root,value='')\n",
    "entryPwd=tkinter.Entry(root,\n",
    "                        show='*',   #不管输入什么，都显示星号\n",
    "                        width=80,\n",
    "                        textvariable=varPwd)\n",
    "entryPwd.place(x=100,y=30,width=80,height=20)\n",
    "\n",
    "#尝试自动填写用户名和密码\n",
    "try:\n",
    "    with open(filename) as fp:\n",
    "        n,p=fp.read().split(',')\n",
    "        varName.set(n)\n",
    "        varPwd.set(p)\n",
    "except:\n",
    "    pass\n",
    "\n",
    "#记住我，复选框\n",
    "rememberMe=tkinter.IntVar(root,value=1)\n",
    "#选中是变量值为1，未选中时变量值为0，默认选中\n",
    "checkRemember=tkinter.Checkbutton(root,\n",
    "                                   text='Remember Me',\n",
    "                                   variable=rememberMe,\n",
    "                                   onvalue=1,\n",
    "                                   offvalue=0)\n",
    "checkRemember.place(x=100,y=70,width=120,height=20)\n",
    "\n",
    "#登录按钮时间处理函数\n",
    "def login():\n",
    "    #获取用户名和密码\n",
    "    name=entryName.get()\n",
    "    pwd=entryPwd.get()\n",
    "    if name=='admin' and pwd=='123456':\n",
    "        tkinter.messagebox.showinfo(title='恭喜',\n",
    "                                    message='登陆成功！')\n",
    "        if rememberMe.get()==1:\n",
    "        #把登陆成功的信息写入临时文件\n",
    "              with open(filename,'w') as fp:\n",
    "                  fp.write(','.join((name,pwd)))\n",
    "    else:\n",
    "        try:\n",
    "            #删除用于记录用户名和密码的临时文件\n",
    "            os.remove(filename)\n",
    "        except:\n",
    "            pass\n",
    "        else:\n",
    "            tkinter.messagebox.showerror(title='警告',\n",
    "                                        message='用户名或密码错误')\n",
    "    #创建按钮组件，同时设置按钮事件处理函数\n",
    "    buttonOk=tkinter.Button(root,\n",
    "                        text='Login', #设置按钮上显示的文本\n",
    "                        command=login) #设置按钮的单击事件处理函数\n",
    "    buttonOk.place(x=30,y=100,width=50,height=20)\n",
    "    \n",
    "    #取消按钮的事件处理函数\n",
    "    def cancel():\n",
    "        #清空用户输入的用户名和密码\n",
    "        varName.set('')\n",
    "        varPwd.set('')\n",
    "        buttonCancel=tkinter.Button(root,\n",
    "                        text='Cancel',\n",
    "                        command=cancel)\n",
    "        buttonCancel.place(x=90,y=100,width=50,height=20)\n",
    "\n",
    "        #启动消息循环\n",
    "        root.mainloop()\n",
    "    \n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
